# -*- coding: utf-8 -*-
"""
 @Time    : 2021/2/3 下午5:19
 @Author  : tgy
 
"""

class Open:
    def __init__(self, path):
        self.path = path

    def __enter__(self):
        self.file = open(self.path)
        return self.file

    def __exit__(self, exc_type, exc_val, exc_tb):
        ...
        if not exc_type:
            ...
        else:
            ...
        self.file.close()
        # 这里返回的True/False来控制是否抛出异常
        return False


def test01():
    ...
    with Open(path='./demo01.py') as f:
        ...
        for line in f:
            ...
            raise IndexError
            print(line)

if __name__ == '__main__':
    ...
    test01()